import {
    CreatePunch,
    GetSubCategory,
    GetTravelDetail,
    GetTravels,
    GetUserTravels,
    GetUserTravelsDetail
} from "../../model/travels";
import {getSystemSize} from "../../utils/system"

Page({
    data: {
        locationList: [],
        current: 0,
        currentItem: {},
        punchList: [],
        topCurrent: 0,
        arr: [],
        category: '',
        startMaskShow:false
    },
    category: '',
    onLoad: async function (options) {
        // getSystemSize().then(res => {
        //     console.log(res.screenHeight)
        // })
        this.category = options.category
        wx.setNavigationBarTitle({
            title: this.category
        })
        this.setData({
            category: this.category
        })
        GetTravels({
            category: this.category
        }).then(res => {
            this.setData({
                locationList: res.data,
                currentItem: res.data[0]
            })
            GetSubCategory({
                category: this.category
            }).then(res => {
                console.log(res)
                this.setData({
                    arr: res.data
                })
            })
        })
        const count = wx.getStorageSync('walk')
        if (count) {
            if (count < 3) {
                this.setData({
                    startMaskShow: true
                })
            }
        }else {
            this.setData({
                startMaskShow: true
            })
            wx.setStorageSync('walk', 1)
        }
    },
    onKnow() {
        this.setData({
            startMaskShow: false
        })
        const count = wx.getStorageSync('walk')
        wx.setStorageSync('walk', count + 1)
    },
    onShow() {
        GetUserTravelsDetail().then(res => {
            this.setData({
                punchList: res
            })
        })
    },
    onSwiperChange(e) {
        const item = this.data.locationList[e.detail.current]
        let idx = this.data.arr.findIndex(v => item.sub_category == v)
        this.setData({
            current: e.detail.current,
            topCurrent: idx,
            currentItem: this.data.locationList[e.detail.current]
        })
    },
    onTopSwiperChange(e) {
        console.log(e)
        const item = this.data.arr[e.detail.current]
        const currentIdX = this.data.locationList.findIndex(v => v.sub_category == item)
        this.setData({
            topCurrent: e.detail.current,
            current: currentIdX,
            currentItem: this.data.locationList[currentIdX]
        })
    },
    onTopItemTap(e) {
        const item = e.currentTarget.dataset.item
        const topIdx = this.data.arr.findIndex(v => v == item)
        const currentIdX = this.data.locationList.findIndex(v => v.sub_category == item)
        this.setData({
            topCurrent: topIdx,
            current: currentIdX,
            currentItem: this.data.locationList[currentIdX]
        })
    },
    onDetailTap() {
        // wx.navigateTo({
        //     url: `/pages/travelDetail/index?id=${this.data.currentItem.id}&title=${this.data.currentItem.title}`
        // })
        wx.showLoading()
        CreatePunch(this.data.currentItem.id, this.data.currentItem.category).then(res => {
            wx.hideLoading()
            if (res) {
                wx.navigateTo({
                    url: `/pages/travelDetail/index?id=${this.data.currentItem.id}&title=${this.data.currentItem.title}`
                })
            }
        }).catch(_ => {
            wx.hideLoading()
        })
    },
    onDetail() {
        wx.navigateTo({
            url: `/pages/travelDetail/index?id=${this.data.currentItem.id}&title=${this.data.currentItem.title}`
        })
    },
    onRightTap() {
        this.setData({
            current: this.data.current + 1
        })
    },
    onLeftTap() {
        this.setData({
            current: this.data.current - 1
        })
    },
    catchTouchMove:function(){
        return false
    },
});
